/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package onLineCmd;

import java.sql.*;

/**
 *
 * @author boomar
 */
public class ConnectBDD {

    private String driver = "com.mysql.jdbc.Driver";
    private String url = "jdbc:mysql://172.19.4.2:3306";
    private String user = "CAR1";
    private String password = "CAR1";

    public ConnectBDD() {
    }

    public ConnectBDD(String driver, String url, String user, String password) {
        this.driver = driver;
        this.url = url;
        this.user = user;
        this.password = password;
    }

    public ResultSet execQuery(String query) throws SQLException {

        try {
            Class.forName(this.driver);
        } catch (Exception ex) {
            return null;
        }

        Connection cx = DriverManager.getConnection(this.url, this.user, this.password);
        Statement st = cx.createStatement();

        try {
            ResultSet rs = st.executeQuery(query);
            rs.close();
            return rs;
        } catch (SQLException ex) {
            return null;
        } finally {
            st.close();
            cx.close();
            return null;
        }
    }

    public ListeArticles getListeArticles() throws SQLException {

        ListeArticles listeArticles = new ListeArticles();

        ResultSet rs = execQuery("SELECT * FROM ED_JDBC.articles");

        while (rs.next()) {
                listeArticles.add(new Article(rs.getInt("id"),rs.getDouble("prix"), rs.getString("nom"), rs.getString("Description")));
        }
        
        return listeArticles;
    }
}
